<?php

class Properties {

    private $_ID;
    private $_name;

    public function getID() {
        return $this->_ID;
    }

    public function setID($ID) {
        $this->_ID = $ID;
    }

    public function getName() {
        return $this->_name;
    }

    public function setName($Name) {
        $this->_name = $Name;
    }

    /**
     * get property list
     * @param type $db
     * @return \Properties|null
     */
    public function getList($db = null) {
        if ($db != null) {

            $sql = "SELECT
                        `P_ID`,
                        `P_Name`
                      FROM `properties`
                      ORDER BY P_ID DESC";

            $parameters = null;
            $values = null;
            $types = null;
            $result = $db->executeQuery($sql, $parameters, $values, $types);

            // nếu có kết quả
            if ($result != null) {
                // mảng danh sách trả về
                $rows = array();
                // lấy mỗi dòng dữ liệu đưa vào mảng danh sách trả về
                while ($row = $result->fetch()) {
                    $property = new Properties();
                    $property->setID($row->P_ID);
                    $property->setName($row->P_Name);
                    $rows[] = $property;
                }
                return $rows;
            } else {
                return null;
            }
        }
    }

    public function getByID($db = null) {
        if ($db != null) {
            $sql = "SELECT
                        `P_ID`,
                        `P_Name`
                      FROM `properties`
                      WHERE P_ID = :P_ID";

            $parameters = array(':P_ID');
            $values = array($this->getID());
            $types = array(PDO::PARAM_INT);

            $result = $db->executeQuery($sql, $parameters, $values, $types);

            // nếu có kết quả
            if ($result != null) {
                $row = $result->fetch();
                $this->setID($row->P_ID);
                $this->setName($row->P_Name);
                return $this;
            } else {
                return null;
            }
        }
    }

    public function add($db) {
        $rs = false;
        if ($db != null) {
            $sqlQuery = "INSERT INTO properties (P_Name) VALUES (:P_Name);";

            $parameters = array(":P_Name");

            $values = array($this->getName());

            $types = array(PDO::PARAM_STR);

            return $db->executeNonQuery($sqlQuery, $parameters, $values, $types);
        }
    }

    public function delete($db) {
        $rs = false;
        if ($db != null) {
            $sqlQuery = "DELETE FROM properties WHERE P_ID = :P_ID";

            $parameters = array(":P_ID");

            $values = array($this->getID());

            $types = array(PDO::PARAM_INT);

            return $db->executeNonQuery($sqlQuery, $parameters, $values, $types);
        }
    }

    public function update($db) {
        $rs = false;
        if ($db != null) {
            $sqlQuery = "UPDATE properties SET P_Name = :P_Name WHERE P_ID = :P_ID ;";

            $parameters = array(":P_Name", ":P_ID");

            $values = array($this->getName(), $this->getID());

            $types = array(PDO::PARAM_STR, PDO::PARAM_INT);

            return $db->executeNonQuery($sqlQuery, $parameters, $values, $types);
        }
    }

}

?>
